<template>
  <div class="page-wrapper">
    <div class="login-container">
      <div class="circle-border">
        <h2>Login</h2>
        <div class="input-box">
          <input v-model="loginForm.username" type="text" placeholder="Username" required />
        </div>
        <div class="input-box">
          <input v-model="loginForm.password" type="password" placeholder="Password" required />
        </div>
        <div class="forgot-pass">
          <a href="#">Forgot your password?</a>
        </div>
        <button type="submit" class="login-btn" @click="handleLogin" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
        <div class="signup-link">
          <a href="#" @click.prevent="goRegister">Go to Register</a>
        </div>
      </div>

      <div class="ticks-container">
        <span v-for="n in 50" :key="n" :style="{ '--i': n - 1 }"></span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { login } from "@/api/login.ts";
import { log } from 'console';

const router = useRouter();

// 定义登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
});

// 加载状态
const loading = ref(false);

// 登录方法
async function handleLogin() {

  // 去除输入框的前后空格
  const username = loginForm.username.trim();
  const password = loginForm.password.trim();

  // 表单验证
  if (!username || !password) {
    ElMessage.error('请输入用户名和密码');
    return;
  }


  // 发送登录请求使用去除空格后的值
  const { data } = await login({
    username,
    password
  });

  console.log(data);

  if (data.code == 200 && data.msg == '登陆成功') {
    router.push('homeview');
    // 登录成功
    // 存储用户信息
    localStorage.setItem('authorization', data.data);
    localStorage.setItem('username', username);
    localStorage.setItem('password', password);
   
    // 显示成功弹窗
    ElMessage({
      message: '登录成功，欢迎回来！',
      type: 'success',
      duration: 3000,
      showClose: true,
      customClass: 'custom-message'
    });
   
 
  } else {
    // 登录失败
    ElMessage({
      message: data.msg || '登录失败，请检查用户名和密码',
      type: 'error',
      duration: 3000,
      showClose: true,
      customClass: 'custom-message'
    });
  }

}

// 跳转到注册页面
function goRegister() {
  console.log('跳转到注册页面');
  router.push('/register');
}

</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.page-wrapper {
  width: 100vw;
  height: 100vh;
  background-image: url('../assets/背景图.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
}

.login-container {
  position: absolute;
  width: 500px;
  height: 500px;
  right: 5%;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.circle-border {
  position: absolute;
  width: 300px;
  padding: 30px;
  z-index: 10;
  /* background: rgba(16, 24, 39, 0.7);
  backdrop-filter: blur(8px); */
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
}

.ticks-container {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.ticks-container span {
  position: absolute;
  left: 0;
  width: 32px;
  height: 6px;
  background: #2c4766;
  border-radius: 80px;
  transform-origin: 250px;
  transform: rotate(calc(var(--i) * (360deg / 50)));
  animation: blink 3s linear infinite;
  animation-delay: calc(var(--i) * (3s / 50));
}

@keyframes blink {
  0% {
    background: #0ef;
  }

  25% {
    background: #2c4766;
  }
}

h2 {
  color: #0ef;
  font-size: 2.5em;
  margin-bottom: 25px;
  font-weight: 500;
  text-align: center;
}

.input-box {
  width: 100%;
  margin-bottom: 15px;
}

.input-box input {
  width: 100%;
  padding: 12px 15px;
  background: rgba(30, 41, 59, 0.4);
  border: 1px solid rgba(0, 238, 255, 0.2);
  border-radius: 25px;
  color: #fff;
  font-size: 16px;
  outline: none;
  transition: all 0.3s ease;
}

.input-box input:focus {
  border-color: #0ef;
  box-shadow: 0 0 10px rgba(0, 238, 255, 0.3);
}

.input-box input::placeholder {
  color: rgba(255, 255, 255, 0.7);
}

.forgot-pass {
  width: 100%;
  text-align: right;
  margin-bottom: 15px;
}

.forgot-pass a {
  color: rgba(255, 255, 255, 0.7);
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.forgot-pass a:hover {
  color: #0ef;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: #0ef;
  border: none;
  border-radius: 25px;
  color: #1a1a1a;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 15px;
}

.login-btn:hover {
  background: #00c4d4;
  transform: translateY(-2px);
}

.signup-link {
  text-align: center;
  width: 100%;
}

.signup-link a {
  color: #0ef;
  text-decoration: none;
  font-size: 16px;
  transition: color 0.3s ease;
}

.signup-link a:hover {
  color: #00c4d4;
}

/* 全局样式，需要添加到App.vue或全局样式文件中 */
:deep(.custom-message) {
  min-width: 300px;
  padding: 15px 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(8px);
  font-weight: 500;
}

:deep(.el-message--success) {
  background: rgba(0, 238, 255, 0.15);
  border-color: #0ef;
}

:deep(.el-message--error) {
  background: rgba(255, 99, 71, 0.15);
  border-color: tomato;
}

/* 登录按钮加载状态 */
.login-btn {
  position: relative;
  overflow: hidden;
}

.login-btn:disabled {
  background: #65c9d1;
  cursor: not-allowed;
}

.login-btn:disabled::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg,
      transparent,
      rgba(255, 255, 255, 0.2),
      transparent);
  animation: loading 1.5s infinite;
}

@keyframes loading {
  0% {
    left: -100%;
  }

  100% {
    left: 100%;
  }
}
</style>